//
//  EditInfoController.m
//  YunDongGym
//
//  Created by lqq on 2018/5/29.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "EditInfoController.h"
@interface EditInfoController () <UITextFieldDelegate>
@property (nonatomic, assign) EditInfoType editType;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) NSString *originStr;

@end

@implementation EditInfoController

#pragma mark Class Method
+ (instancetype)controllerWithEditType:(EditInfoType)editType originStr:(NSString *)originStr;
{
    EditInfoController *editVC = [[EditInfoController alloc] init];
    editVC.editType = editType;
    editVC.originStr = originStr;
    return editVC;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self configUI];
}


#pragma mark Config/UI
- (void)configUI
{
    
    UITextField *textField = [[UITextField alloc] init];
    textField.textColor = HEXColor(@"#333333");
    [self.view addSubview:textField];
    [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    textField.delegate = self;
    _textField = textField;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(40));
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(self.view).offset(10);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor =HEXColor(@"#3AA7FF");
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(kOnePX));
        make.left.right.equalTo(textField);
        make.top.equalTo(textField.mas_bottom);
    }];
    
    switch (_editType) {
        case EditInfoType_GymMobile:
        {
            self.title = @"门店电话";
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.text = _originStr;
        }
            break;
        case EditInfoType_BrandName:
        {
            self.title = @"健身房名称";
            textField.text = _originStr;
        }
            break;
        case EditInfoType_BrandCreator:
        {
            self.title = @"管理员名称";
            textField.text = _originStr;
        }
            break;
        case EditInfoType_AdminAccount:
        {
            self.title = @"店长账号";
            textField.keyboardType = UIKeyboardTypeEmailAddress;
            textField.text = _originStr;
        }
            break;
        case EditInfoType_AdminAccountPWD:
        {
            self.title = @"添加密码";
            textField.keyboardType = UIKeyboardTypeEmailAddress;
            textField.text = _originStr;
            textField.placeholder = @"密码长度需大于6位，只允许英文+数字结合";

        }
            break;

        case EditInfoType_AdminName:
        {
            self.title = @"店长姓名";
            textField.text = _originStr;
        }
            break;
        case EditInfoType_AdminMobile:
        {
            self.title = @"店长电话";
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.text = _originStr;
            
        }
            break;
        default:
            break;
    }

    [textField becomeFirstResponder];
    
    [self setRightBarButtonWithTitle:@"保存" target:self action:@selector(saveInfo)];
}

- (void)saveInfo
{
    NSString *text = _textField.text;
    if (text.length==0) {
        [AV showRemind:@"请输入内容"];
        return;
    }
    switch (_editType) {
        default:
        {
            if (_editBlock) {
                _editBlock(text);
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
            break;
    }
    
    
}

#pragma mark - KVO
- (void)textFieldChanged:(UITextField *)textField
{
    switch (_editType) {
        case EditInfoType_GymMobile:
        case EditInfoType_AdminMobile:
        {
            if (textField.text.length >= 11) {
                textField.text = [textField.text substringToIndex:11];
            }
        }
            break;
            
        default:
            break;
    }
    
}




@end
